package ningbo.design;

/**
 * @Author 宁铂
 * @Date 2021/7/29 10:08
 * @Version 1.0
 */
public class TestSingle2 {
    /** 单例设计模式2 -- 懒汉式 */
    public static void main(String[] args) {
        MySingle2 single2 = MySingle2.getSingle2();
        MySingle2 single3 = MySingle2.getSingle2();
        System.out.println(single2 == single3);

    }
}

class MySingle2{

    private MySingle2(){}

    /** 懒汉式: 通过创建的成员变量的值是否尾null来判断是否已经创建对象 */
    private static MySingle2  single2;

    public static MySingle2 getSingle2(){

        /** 判断成员变量是否为默认值,只有调用方法时才会首次创建对象 */
        if (single2 == null){

            single2 = new MySingle2();
        }

        return single2;
    }
}